part of '../repository.dart';

const _kTransactionRevocationInfoTimeKey = 'time';
const _kTransactionRevocationInfoReasonKey = 'reason';

class _TransactionRevocationInfoDto extends Equatable {
  const _TransactionRevocationInfoDto._internal({
    required this.timeDto,
    required this.reasonDto,
  });

  final _TimeDto timeDto;
  final _TransactionRevocationReasonDto reasonDto;

  @override
  List<Object?> get props {
    return [
      timeDto,
      reasonDto,
    ];
  }

  factory _TransactionRevocationInfoDto.fromJson(Map<String, dynamic> json) {
    final timeDto = _TimeDto.fromString(json[_kTransactionRevocationInfoTimeKey]);
    final reasonDto =
        _TransactionRevocationReasonDto.fromString(json[_kTransactionRevocationInfoReasonKey]);

    return _TransactionRevocationInfoDto._internal(
      timeDto: timeDto,
      reasonDto: reasonDto,
    );
  }

  factory _TransactionRevocationInfoDto.fromEntity(
      AppStoreTransactionRevocationInfo revocationInfo) {
    final timeDto = _TimeDto.fromEntity(revocationInfo.time);
    final reasonDto = _TransactionRevocationReasonDto.fromEntity(revocationInfo.reason);

    return _TransactionRevocationInfoDto._internal(
      timeDto: timeDto,
      reasonDto: reasonDto,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kTransactionRevocationInfoTimeKey: timeDto.toString(),
      _kTransactionRevocationInfoReasonKey: reasonDto.toString(),
    };
  }

  AppStoreTransactionRevocationInfo toEntity() {
    return AppStoreTransactionRevocationInfo(
      time: timeDto.toEntity(),
      reason: reasonDto.toEntity(),
    );
  }
}
